﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace FileRusher
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        String path = @"c:\Users\loverxp\AppData\Roaming\Microsoft\Windows\Recent\";

        public Window1()
        {
            InitializeComponent();
            //Init();
/*
            testListBox.Items.Add("abc");
            testListBox.Items.Add("xyz");
            testListBox.Items.Add("haha");
*/

            InitFileList();
            startWatcher();
        }

        private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                for (int i = 0; i < testListBox.Items.Count; i++)
                {
                    String str;
                    try
                    {
                        ListBoxItem item = (ListBoxItem)testListBox.Items[i];
                        str = item.ToString();
                    }
                    catch (System.Exception ex)
                    {
                        str = (String)testListBox.Items[i];
                    }
                    if (-1 != str.IndexOf(textBox1.Text))
                    {
                        testListBox.SelectedIndex = i;
                        return;
                    }
                }
            }
            catch (System.Exception ex)
            {

                //statusLabel
            }
            finally
            {
            }
        }

        private void InitFileList()
        {

            DirectoryInfo directory = new DirectoryInfo(path);
            foreach (FileInfo file in directory.GetFiles())
            {
                testListBox.Items.Add(file.Name);
            }
        }

        private void testListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            return;
            try
            {
                textBox1.Text = (String)testListBox.SelectedItem;
                textBox1.Focus();
                textBox1.SelectionStart = textBox1.Text.Length;
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void startWatcher()
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            //watcher.Filter = "*.txt";
            watcher.Created += new FileSystemEventHandler(watcher_FilesCreated);
            watcher.Deleted += new FileSystemEventHandler(watcher_FilesDeleted);
            watcher.Path = path;
            watcher.EnableRaisingEvents = true;
        }

        private void watcher_FilesCreated(object sender, FileSystemEventArgs e)
        {
            //Console.WriteLine("A new *.txt file has been created!");
            MessageBox.Show("A new *.txt file has been created!");
        }

        private void watcher_FilesDeleted(object sender, FileSystemEventArgs e)
        {
            //Console.WriteLine("A new *.txt file has been deleted!");
            MessageBox.Show("A new *.txt file has been deleted!");
        }

        private void executeFile(String fileName)
        {
            if (File.Exists(fileName))
            {
                System.Diagnostics.Process.Start(fileName);
            }
            else
            {
                MessageBox.Show("file" + fileName + "is not exists!");
            }
        }

        private void testListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                String fileName = (String)testListBox.SelectedItem;
                String fullFileName = path + fileName;
                executeFile(fullFileName);
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private bool isLnkFile(String fileName)
        {
            return false;
        }


    }
}
